const router = require('koa-router')()
const Mock = require('mockjs')

router.prefix('/home')

const swiperListData = Mock.mock({
  'result|3': [{
    'id|+1': 1,
    'image': '@image(750x300, @color)'
  }]
})

const hotCounty = Mock.mock({
  'result|10-15': [{
    'id': '@id',
    'county': '@county',
    'image': '@image(100x100, @color)'
  }]
})

const featureList = Mock.mock({
  'result|100': [{
    'id|+1': 1000,
    'image': '@image(750x240, @color)',
    'region': '@region',
    'name': '@county(true)',
    'desc': '@cparagraph(1,3)'
  }]
})

router.get('/swiper', async (ctx, next) => {
  ctx.body = {
    code: 20000,
    data: swiperListData
  }
})

router.get('/hot', async (ctx, next) => {
  ctx.body = {
    code: 20000,
    data: hotCounty
  }
})

router.get('/list', async (ctx, next) => {
  const pageNo = Number(ctx.query.pageNo)
  const pageSize = Number(ctx.query.pageSize)
  const maxPage = featureList.result.length / pageSize
  const total = featureList.result.length

  // 返回构造对象
  let res = {
    code: 20000,
    data: {
      result: [],
      total,
      hasMore: true
    }
  }

  // 如果超过最大页数
  if ((pageNo*1 + 1) >= maxPage) {
    res.data.hasMore = false
  }
  res.data.result = featureList.result.slice(pageNo * pageSize, pageNo * pageSize + pageSize)
  ctx.body = res
})

module.exports = router